<?php 

class cardModel extends appModel
{
	
    
  
	public function saveInfo($id,$wxid)
	{
	     
	     
	    $this->setInc("is_get", 1, "card_get_record", $id);
	       
	    $record = $this->getGetRecord($id);
	    
	    $integral = $record['change_integral'];
	    
	    
	    $this->setWxInc("integral", -$integral, "wxusers", $wxid);
	    
        $this->setInc("get_total", 1, "card", $record['cid']);
        
	    $record = $this->getWxRecord($wxid);
	    
	    
	    
	    return $record['integral'];
	     
	}
	
	public function setInc($field,$step,$table,$id)
	{
	
	
	    $sql = " update ".$this->table($table)." set ".$field." = ".$field." + ".$step." where id = '".$id."'";
	
	    return $this->query($sql);
	}
	
	public function setWxInc($field,$step,$table,$id)
	{
	
	   
	    $sql = " update ".$this->table($table)." set ".$field." = ".$field." + ".$step." where wxid = '".$id."'";
	       
	    return $this->query($sql);
	}
	
	/**
	 *获得一条微信record记录
	 */
	public function getWxRecord($id)
	{
	    if(!$id)
	    {
	        return false;
	    }
	    $sql = "SELECT *,nickname as name FROM ".$this->table('wxusers')." WHERE wxid =".$id;
	    $result=$this->getRow($sql);
	   
	   
	    
	    if($result)
	    {
	
	        return $result;
	    }
	    else
	    {
	        return false;
	    }
	}
	
	

	
public function is_saved_user($wxid){
    
    
		$sql = "SELECT COUNT(*) FROM ".$this->table('card_user')." WHERE wxid = '$wxid'";
		$count = $this->getOne($sql);
		if($count){
			return true;
		}else{
			return false;
		}
		
	}
	
	public function is_saved_card($cid,$wxid){
	
	   
	    $sql = "SELECT count(*) FROM ".$this->table('card_get_record')." WHERE wxid = '$wxid' and cid = '$cid' and is_get =1 ";
	     
	    $count = $this->getOne($sql);
	    if($count){
	        return true;
	    }else{
	        return false;
	    }
	
	}
		
	public function getGetRecord($id)
	{
	    if(!$id)
	    {
	        return false;
	    }
	    $sql = " SELECT p1.change_integral,p1.cid FROM ".$this->table('card_get_record')." AS p1  WHERE p1.id ='".$id."'";
	    
	    $result=$this->getRow($sql);
	
	    if($result)
	    {
	
	        return $result;
	    }
	    else
	    {
	        return false;
	    }
	}
	
	
	
	
	
	public function getCard($id)
	{
	    
	    $res = array("error"=>1,"content"=>"");
	    
	    $sql = " SELECT p1.* FROM ".$this->table('card')." AS p1  WHERE p1.id ='".$id."'";
	    
	    $result = $this->getRow($sql);
	    
	    $day1 =  strtotime(date('Y-m-d 00:00:00'));
	    
	    $hour1 = strtotime(date('Y-m-d H:i'))-date('i')*60;
	    
	    $minute1 = strtotime(date('Y-m-d H:i'));
	    
	    $day2 = strtotime(date("Y-m-d 23:59:59"));
	    
	    $hour2 = $hour1+60*60;
	    
	    $minute2 = $minute1 + 60;
	    
	   
	    
	    
	    
	    
	    if(!$result)
	    {
	        $res['content'] = "该卡券不存在";
	    }
	    else
	    {

	        $sql = " SELECT count(*) FROM  ".$this->table('card_get_record').
	        "  WHERE cid = '$id' AND  addtime > '".$day1."' AND addtime < '".$day2."'";
	        
	         
	        $dcount = $this->getOne($sql);
	        
	         
	        if($dcount>=$result['day_max'])
	        {
	             
	            $res['error'] = 3;
	            $res['content'] = "今日已领取完了，请明日再来";
	            return $res;
	        }
	        
	        
	        $sql = " SELECT count(*) FROM  ".$this->table('card_get_record').
	               "  WHERE cid = '$id' AND  addtime > '".$hour1."' AND addtime < '".$hour2."'";
	         
	         

	        
	        $hcount = $this->getOne($sql);
	         
	        
	        if($hcount>=$result['hour_max'])
	        {
	            
	            
	            
	            $res['error'] = 3;
	            $res['content'] = "当前太多人领取啦，待会再领取吧。";
	            return $res;
	        }
	        
	        $sql = " SELECT count(*) FROM  ".$this->table('card_get_record').
	        "  WHERE cid = '$id' AND  addtime > '".$minute1."' AND addtime < '".$minute2."'";
	        
	        
	        $mcount = $this->getOne($sql);
	        
	        
	        if($mcount>=$result['min_max'])
	        {
	            $res['error'] = 3;
	            $res['content'] = "当前太多人领取啦，待会再领取吧。";
	            return $res;
	        }
	        
	        $res['error'] = 0;
	        $res['data'] = $result;
	    }
	    
	    
	    return $res;
	   
	}
	
	
	public function getChatCard()
	{
	     
	    
	    $sql = " SELECT * FROM ".$this->table('card')." AS p1  WHERE p1.is_chat ='1' LIMIT 1 ";
	     
	    $result = $this->getRow($sql);

	    if($result)
	    {
	        $res['error'] = 0;
	        $res['data'] = $result;
	    }
	    else
	    {
	        $res['error'] = 3;
	        $res['content'] = "小伙伴太热情~今天的卡券已经领完了!";
	    }
	    
	    return $res;
	
	}
	
	public function getCardList()
	{
	     
	    $sql = " SELECT p1.* FROM ".$this->table('card')." AS p1  WHERE  status = 1 and is_chat = '0'  ";

// 	    $sql = " SELECT p1.* FROM ".$this->table('card')." AS p1  WHERE  status = 1  ";
	    
	    
	    $result = $this->getAll($sql);

	    foreach ($result as $k => $v)
	    {
	        $result[$k]['dotime2'] = date("Y-m-d",$v['dotime2']);
	    }
	    
	    return $result;
	
	}
	
	public function insertCardUser($user)
	{
	    
	    $wxid = $user['wxid'];
	    
	    $cid = $user['cid'];
	    
	    $sql = "SELECT * FROM ".$this->table('card_get_record')." WHERE wxid = '$wxid' and cid = '$cid' ";
	    
	    $record = $this->getRow($sql);
	    
	    if(!$record){
	        
    	    $id = $this->insert($user,"card_get_record");
	    }
	    else if($record&&$record['is_get']==0)
	    {
	        
	        $id = $record['id'];
	        
	        $this->setInc("num", 1, "card_get_record", $id);
	        
	    }
	    else
	    {
	        $id = "-1";
	    }

	    
	    return $id;
	    
	}
	
}

?>
